﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using com.paypal.sdk.util;
using ECSE6770.Payment;

namespace ECSE6770
{
    public static class Extensions
    {
        public static void RemoveWhere<T>(this DbSet<T> items, Predicate<T> pred) where T : class
        {
            List<T> toRemove = new List<T>();
            foreach (T item in items)
            {
                if (pred(item))
                    toRemove.Add(item);
            }
            foreach (T item in toRemove)
            {
                items.Remove(item);
            }
        }

        public static IEnumerable<SelectListItem> AddPleaseSelectItem(this IEnumerable<SelectListItem> items)
        {
            return items.AppendItem("Please Select", "0");
        }

        public static IEnumerable<SelectListItem> AppendItem(this IEnumerable<SelectListItem> items, string text, string value)
        {
            return new List<SelectListItem> { new SelectListItem { Text = text, Value = value } }.Union(items);
        }

        public static MvcHtmlString LabelForWithReq<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
            string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
            string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
            if (String.IsNullOrEmpty(labelText))
            {
                return MvcHtmlString.Empty;
            }

            TagBuilder tag = new TagBuilder("label");
            tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));
            tag.SetInnerText(labelText);
            if (metadata.IsRequired)
            {
                TagBuilder req = new TagBuilder("span");
                req.AddCssClass("req");
                req.SetInnerText("*");
                tag.InnerHtml += " " + req.ToString();
            }
            return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString CountryDropDownFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
        {
            List<SelectListItem> items = new List<SelectListItem>
            {
                new SelectListItem { Text = "United States", Value = "United States" }
            };
            items.AddRange(CountryManager.CountryList.Select(c => new SelectListItem { Text = c.Name, Value = c.Name }));
            return html.DropDownListFor(expression, items);
        }

        public static MvcHtmlString StateDropDownFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
        {
            List<SelectListItem> items = new List<SelectListItem>
            {
                new SelectListItem { Value = "", Text = "Please Select" },
                new SelectListItem { Value = "AL", Text = "Alabama" },
                new SelectListItem { Value = "AK", Text = "Alaska" },
                new SelectListItem { Value = "AZ", Text = "Arizona" },
                new SelectListItem { Value = "AR", Text = "Arkansas" },
                new SelectListItem { Value = "CA", Text = "California" },
                new SelectListItem { Value = "CO", Text = "Colorado" },
                new SelectListItem { Value = "CT", Text = "Connecticut" },
                new SelectListItem { Value = "DE", Text = "Delaware" },
                new SelectListItem { Value = "DC", Text = "District of Columbia" },
                new SelectListItem { Value = "FL", Text = "Florida" },
                new SelectListItem { Value = "GA", Text = "Georgia" },
                new SelectListItem { Value = "HI", Text = "Hawaii" },
                new SelectListItem { Value = "ID", Text = "Idaho" },
                new SelectListItem { Value = "IL", Text = "Illinois" },
                new SelectListItem { Value = "IN", Text = "Indiana" },
                new SelectListItem { Value = "IA", Text = "Iowa" },
                new SelectListItem { Value = "KS", Text = "Kansas" },
                new SelectListItem { Value = "KY", Text = "Kentucky" },
                new SelectListItem { Value = "LA", Text = "Louisiana" },
                new SelectListItem { Value = "ME", Text = "Maine" },
                new SelectListItem { Value = "MD", Text = "Maryland" },
                new SelectListItem { Value = "MA", Text = "Massachusetts" },
                new SelectListItem { Value = "MI", Text = "Michigan" },
                new SelectListItem { Value = "MN", Text = "Minnesota" },
                new SelectListItem { Value = "MS", Text = "Mississippi" },
                new SelectListItem { Value = "MO", Text = "Missouri" },
                new SelectListItem { Value = "MT", Text = "Montana" },
                new SelectListItem { Value = "NE", Text = "Nebraska" },
                new SelectListItem { Value = "NV", Text = "Nevada" },
                new SelectListItem { Value = "NH", Text = "New Hampshire" },
                new SelectListItem { Value = "NJ", Text = "New Jersey" },
                new SelectListItem { Value = "NM", Text = "New Mexico" },
                new SelectListItem { Value = "NY", Text = "New York" },
                new SelectListItem { Value = "NC", Text = "North Carolina" },
                new SelectListItem { Value = "ND", Text = "North Dakota" },
                new SelectListItem { Value = "OH", Text = "Ohio" },
                new SelectListItem { Value = "OK", Text = "Oklahoma" },
                new SelectListItem { Value = "OR", Text = "Oregon" },
                new SelectListItem { Value = "PA", Text = "Pennsylvania" },
                new SelectListItem { Value = "RI", Text = "Rhode Island" },
                new SelectListItem { Value = "SC", Text = "South Carolina" },
                new SelectListItem { Value = "SD", Text = "South Dakota" },
                new SelectListItem { Value = "TN", Text = "Tennessee" },
                new SelectListItem { Value = "TX", Text = "Texas" },
                new SelectListItem { Value = "UT", Text = "Utah" },
                new SelectListItem { Value = "VT", Text = "Vermont" },
                new SelectListItem { Value = "VA", Text = "Virginia" },
                new SelectListItem { Value = "WA", Text = "Washington" },
                new SelectListItem { Value = "WV", Text = "West Virginia" },
                new SelectListItem { Value = "WI", Text = "Wisconsin" },
                new SelectListItem { Value = "WY", Text = "Wyoming" }
            };
            return html.DropDownListFor(expression, items);
        }

        public static MvcHtmlString MonthDropDownFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
        {
            List<SelectListItem> items = new List<SelectListItem>
            {
                new SelectListItem { Value = "01", Text = "01" },
                new SelectListItem { Value = "02", Text = "02" },
                new SelectListItem { Value = "03", Text = "03" },
                new SelectListItem { Value = "04", Text = "04" },
                new SelectListItem { Value = "05", Text = "05" },
                new SelectListItem { Value = "06", Text = "06" },
                new SelectListItem { Value = "07", Text = "07" },
                new SelectListItem { Value = "08", Text = "08" },
                new SelectListItem { Value = "09", Text = "09" },
                new SelectListItem { Value = "10", Text = "10" },
                new SelectListItem { Value = "11", Text = "11" },
                new SelectListItem { Value = "12", Text = "12" }
            };
            return html.DropDownListFor(expression, items);
        }

        public static MvcHtmlString YearRangeDropDownFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, int range)
        {
            List<SelectListItem> items = new List<SelectListItem>();

            int current = DateTime.Now.Year;
            for (int i = current - range; i <= current + range; i++)
                items.Add(new SelectListItem { Text = i.ToString(), Value = i.ToString(), Selected = current == i });

            return html.DropDownListFor(expression, items);
        }
    }
}